﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Eniux.Windows.EnterpriseLibrary.Commands;

namespace EventCommandDemo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public ObservableCollection<EmailMessage> Emails { get; private set; }
        public MainWindow()
        {
            Emails = new ObservableCollection<EmailMessage>();
            Emails.Add(new EmailMessage() { From = new Contact() { DisplayName = "reader1@blog.net" }, Subject = "I have a question regards command trigger", Received = new DateTime(2009, 4, 3, 13, 42, 0) });
            Emails.Add(new EmailMessage() { From = new Contact() { DisplayName = "reader2@blog.net" }, Subject = "RE: I have a question regards command trigger", Received = DateTime.Now });

            InitializeCommands();
            InitializeComponent();

            DataContext = this;
            
        }
        public ICommand DownloadEmail { get; private set; }
        public ICommand MarkAsRead { get; private set; }
        public ICommand OpenEmail { get; private set; }
        public ICommand DownloadContact { get; private set; }

        private void InitializeCommands()
        {
            #region DownloadEmail Command
            DownloadEmail = new RoutedCommand();
            CommandBinding cmdBinding1 = new CommandBinding(DownloadEmail);

            cmdBinding1.Executed += (s, e) =>
            {
                var parameter = CommandParameter<EmailMessage>.Cast(e.Parameter);
                parameter.CustomParameter.Download();
            };

            cmdBinding1.CanExecute += (s, e) =>
            {
                var parameter = CommandParameter<EmailMessage>.Cast(e.Parameter);
                e.CanExecute = !parameter.CustomParameter.IsDownloaded;
            };

            CommandBindings.Add(cmdBinding1);
            #endregion

            #region MarkAsRead Command
            MarkAsRead = new RoutedCommand();
            CommandBinding cmdBinding2 = new CommandBinding(MarkAsRead);

            cmdBinding2.Executed += (s, e) =>
            {
                var parameter = CommandParameter<EmailMessage>.Cast(e.Parameter);
                parameter.CustomParameter.MarkAsRead();
            };

            cmdBinding2.CanExecute += (s, e) =>
            {
                var parameter = CommandParameter<EmailMessage>.Cast(e.Parameter);
                e.CanExecute = !parameter.CustomParameter.IsRead;
            };

            CommandBindings.Add(cmdBinding2);
            #endregion

            #region OpenEmail Command
            OpenEmail = new RoutedCommand();
            CommandBinding cmdBinding3 = new CommandBinding(OpenEmail);

            cmdBinding3.Executed += (s, e) =>
            {
                var parameter = EventCommandParameter<EmailMessage, MouseButtonEventArgs>.Cast(e.Parameter);
                if (parameter.EventArgs.ClickCount == 2)
                {
                    parameter.CustomParameter.MarkAsRead();
                    MessageBox.Show(
                        parameter.CustomParameter.Content,
                        parameter.CustomParameter.Subject);
                }
            };

            cmdBinding3.CanExecute += (s, e) =>
            {
                e.CanExecute = true;
            };

            CommandBindings.Add(cmdBinding3);
            #endregion

            #region DownloadContact Command
            DownloadContact = new RoutedCommand();
            CommandBinding cmdBinding4 = new CommandBinding(DownloadContact);

            cmdBinding4.Executed += (s, e) =>
            {
                var parameter = CommandParameter<Contact>.Cast(e.Parameter);
                parameter.CustomParameter.Download();
            };

            cmdBinding4.CanExecute += (s, e) =>
            {
                var parameter = CommandParameter<Contact>.Cast(e.Parameter);
                e.CanExecute = !parameter.CustomParameter.IsDownloaded;
            };

            CommandBindings.Add(cmdBinding4);
            #endregion
        }		
    }
}
